package org.dromara.config.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.config.domain.bo.KyCustomersBo;
import org.dromara.config.domain.vo.KyCustomersVo;
import org.dromara.config.domain.KyCustomers;
import org.dromara.config.mapper.KyCustomersMapper;
import org.dromara.config.service.IKyCustomersService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户信息Service业务层处理
 *
 * @author kuyu
 * @date 2023-12-29
 */
@RequiredArgsConstructor
@Service
public class KyCustomersServiceImpl implements IKyCustomersService {

    private final KyCustomersMapper baseMapper;

    /**
     * 查询客户信息
     */
    @Override
    public KyCustomersVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户信息列表
     */
    @Override
    public TableDataInfo<KyCustomersVo> queryPageList(KyCustomersBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KyCustomers> lqw = buildQueryWrapper(bo);
        Page<KyCustomersVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户信息列表
     */
    @Override
    public List<KyCustomersVo> queryList(KyCustomersBo bo) {
        LambdaQueryWrapper<KyCustomers> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KyCustomers> buildQueryWrapper(KyCustomersBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KyCustomers> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCateId() != null, KyCustomers::getCateId, bo.getCateId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), KyCustomers::getName, bo.getName());
        lqw.eq(bo.getProvinceId() != null, KyCustomers::getProvinceId, bo.getProvinceId());
        lqw.eq(bo.getCityId() != null, KyCustomers::getCityId, bo.getCityId());
        lqw.eq(bo.getDistrictId() != null, KyCustomers::getDistrictId, bo.getDistrictId());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), KyCustomers::getAddress, bo.getAddress());
        lqw.eq(bo.getDeletedAt() != null, KyCustomers::getDeletedAt, bo.getDeletedAt());
        return lqw;
    }

    /**
     * 新增客户信息
     */
    @Override
    public Boolean insertByBo(KyCustomersBo bo) {
        KyCustomers add = MapstructUtils.convert(bo, KyCustomers.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户信息
     */
    @Override
    public Boolean updateByBo(KyCustomersBo bo) {
        KyCustomers update = MapstructUtils.convert(bo, KyCustomers.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KyCustomers entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
